package com.bite.forum.controller;

import com.bite.forum.common.AppResult;
import com.bite.forum.common.ResultCode;
import com.bite.forum.model.Article;
import com.bite.forum.model.ArticleReply;
import com.bite.forum.model.User;
import com.bite.forum.services.IArticleReplyService;
import com.bite.forum.services.IArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.NotNull;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/reply")
@Tag(name = "帖子回复相关接口")
public class ArticleReplyController {
    @Resource
    private IArticleService articleService;

    @Resource
    private IArticleReplyService articleReplyService;

    @Operation(summary = "创建帖子回复")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @Schema(description = "帖子id") @RequestParam(value = "articleId") @NotNull Long articleId,
                            @Schema(description = "回复内容") @RequestParam(value = "content") @NotNull String content) {
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("USER_SESSION");

        if(user.getState() == 1){
            return AppResult.fail(ResultCode.FAILED_USER_BANNED);
        }

        Article article = articleService.selectById(articleId);
        if(article == null || article.getDeleteState() == 1){
            // 记录警告日志
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            // 返回失败结果
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }

        if(article.getState() == 1){
            // 记录警告日志
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            // 返回失败结果
            return AppResult.fail(ResultCode.FAILED_ARTICLE_BANNED);
        }

        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setPostUserId(user.getId());
        articleReply.setContent(content);
        Date date =  new Date();
        articleReply.setCreateTime(date);
        articleReply.setUpdateTime(date);

        articleReplyService.create(articleReply);

        return AppResult.success();
    }

    @Operation(summary = "获取回复列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId (@Schema(description = "帖子Id") @RequestParam("articleId") @NonNull Long articleId) {
        // 校验帖子是否存在
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1) {
            // 返回错误提示
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 调用Service
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        // 返回结果
        return AppResult.success(articleReplies);

    }
}
